<template>
	<view>
		<view class="search_box">
		
			<!-- 基本用法 -->
			<uni-search-bar focus clearButton="always" bgColor="#FFFFFF" placeholder="请输入搜索内容" radius="18" cancelButton="none" @input="input" ></uni-search-bar>	
		</view>
		
		
		<view class="search_list" v-if="searchjy.length !== 0">
			<view class="search_item" v-for="(item,index) in searchjy" :key="index" @click="gotodetails(item)">
				<view class="text_item">{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="18"></uni-icons>
			</view>
		</view>
		
		<div class="history" v-else>
			<div class="history_head">
				<text>搜索历史</text>
				<uni-icons type="trash" size="18" @click="clearsearchhistory"></uni-icons>
			</div>
			<div class="history_buttom">
				<block v-for="(item,index) in rehistorylist" :key="index">
						<uni-tag :text="item" class="history_item" @click="gotogoodlist(item)"></uni-tag>
				</block>
			</div>
		</div>
		
	</view>
	
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				kw: '',
				searchjy: [],
				historylist: []
			}
		},
		onLoad() {
			this.historylist = uni.getStorageSync('kw' || [])
		},
		methods: {
			input (e) {
				clearTimeout(this.timer)
				this.timer = setTimeout( () => {
					this.kw = e
					this.getsearchjy()
				},500)
			},
			async getsearchjy () {
				if (this.kw.trim().length === 0) {
					this.searchjy = []
					return
				}
				const {data: res} = await uni.$http.get('/api/public/v1/goods/qsearch',{query: this.kw})
				if (res.meta.status !== 200) return uni.$showmsg()
				this.searchjy = res.message
				this.addhistory()
			},
			gotodetails (item) {
				uni.navigateTo({
					url: '../shop_details/shop_details?goods_id=' + item.goods_id
				})
			},
			addhistory () {
				// this.historylist.push(this.kw)
				const set = new Set(this.historylist)
				set.delete(this.kw)
				set.add(this.kw)
				this.historylist = Array.from(set)
				uni.setStorageSync('kw',this.historylist)
			},
			clearsearchhistory () {
				this.historylist = []
				uni.setStorageSync('kw',[])
			},
			gotogoodlist (item) {
				uni.navigateTo({
					url: '../shop_list/shop_list?query=' + item
				})
			}
		},
		components: {

		},
		computed: {
			rehistorylist () {
				return [...this.historylist].reverse()
			}
		},
	}
</script>

<style>
.search_box {
	background-color: #C00000;
	position: sticky;
	top: 0;
	z-index: 999;
}
.search_list {
	padding-left: 10px;
	
}
.search_item {	
	display: flex;
	justify-content: space-between;
	align-items: center;
	border-bottom: 1px solid #efefef;
	padding: 13px 0;
}
.text_item {
	font-size: 12px;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
	padding: 3px 0;
}
.history {
	padding: 0 5px;
}
.history_head {
	display: flex;
	justify-content: space-between;
	height: 40px;
	align-items: center;
	border-bottom: 1px solid #efefef;
}
.history_buttom {
	display: flex;
	flex-wrap: wrap;
	margin-top: 5px;
	padding: 0 5px;
}
.history_item {
	margin-top: 5px;
	margin-right: 10px;
}
</style>
